//
//  FMColorPickerView.m
//  FreeMark
//
//  Created by gaopr on 2019/2/18.
//  Copyright © 2019年 gaopr. All rights reserved.
//

#import "FMColorPickerView.h"
#import "UIConfig.h"

@interface FMColorPickerView ()

@property (nonatomic, weak) UISlider *redSlider;
@property (nonatomic, weak) UISlider *greenSlider;
@property (nonatomic, weak) UISlider *blueSlider;
@property (nonatomic, weak) UISlider *alplaSlider;

@property (nonatomic, weak) UILabel *redSliderLabel;
@property (nonatomic, weak) UILabel *greenSliderLabel;
@property (nonatomic, weak) UILabel *blueSliderLabel;
@property (nonatomic, weak) UILabel *alplaSliderLabel;

@end

@implementation FMColorPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        NSMutableArray *sliders = [[NSMutableArray alloc] init];
        
        NSMutableArray *containers = [[NSMutableArray alloc] init];
        
        UIView *redContainer = [[UIView alloc] init];
        redContainer.backgroundColor = [UIColor redColor];
        [self addSubview:redContainer];
        [containers addObject:redContainer];
        
        UIView *greenContainer = [[UIView alloc] init];
        greenContainer.backgroundColor = [UIColor greenColor];
        [self addSubview:greenContainer];
        [containers addObject:greenContainer];
        
        UIView *blueContainer = [[UIView alloc] init];
        blueContainer.backgroundColor = [UIColor blueColor];
        [self addSubview:blueContainer];
        [containers addObject:blueContainer];
        
        UIView *alphaContainer = [[UIView alloc] init];
        [self addSubview:alphaContainer];
        [containers addObject:alphaContainer];
        
        MASViewAttribute *leading = self.mas_top;
        for (UIView *item in containers)
        {
            [item mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(self);
                make.trailing.equalTo(self);
                make.top.mas_equalTo(leading);
            }];
            leading = item.mas_bottom;
        }
        
        [containers.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom);
            make.height.equalTo(containers);
        }];
        
        [self configSilderWithContainer:redContainer title:@"Red:0" titleColor:[UIColor blackColor]];
        self.redSlider = [redContainer viewWithTag:1];
        self.redSliderLabel = [redContainer viewWithTag:2];
        
        [self configSilderWithContainer:greenContainer title:@"Green:0" titleColor:[UIColor blackColor]];
        self.greenSlider = [greenContainer viewWithTag:1];
        self.greenSliderLabel = [greenContainer viewWithTag:2];
        
         [self configSilderWithContainer:blueContainer title:@"Blue:0" titleColor:[UIColor blackColor]];
        self.blueSlider = [blueContainer viewWithTag:1];
        self.blueSliderLabel = [blueContainer viewWithTag:2];
        
        [self configSilderWithContainer:alphaContainer title:@"Alpha:0" titleColor:[UIColor blackColor]];
        self.alplaSlider = [alphaContainer viewWithTag:1];
        self.alplaSliderLabel = [alphaContainer viewWithTag:2];
        
    }
    return self;
}

- (UISlider *)configSilderWithContainer:(UIView *)container
                            title:(NSString *)title
                       titleColor:(UIColor *)titleColor
{
    UISlider *sliderSlider = [[UISlider alloc] init];
    sliderSlider.tag = 1;
    sliderSlider.minimumValue = 0;
    sliderSlider.maximumValue = 1;
    [container addSubview:sliderSlider];
    [sliderSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(container.mas_bottom);
        make.centerX.equalTo(container);
        make.width.equalTo(container).multipliedBy(0.9);
    }];
    [sliderSlider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    
    UILabel *label = [[UILabel alloc] init];
    label.tag = 2;
    [container addSubview:label];
    label.font = [UIFont systemFontOfSize:16];
    label.text = title;
    label.textColor = titleColor;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(container.mas_top).offset(kFMargin);
        make.centerX.equalTo(container);
    }];
    
    return sliderSlider;
}

- (void)setColor:(UIColor *)color
{
    CGFloat r = 0;
    CGFloat g = 0;
    CGFloat b = 0;
    CGFloat a = 0;
    [color getRed:&r green:&g blue:&b alpha:&a];
    self.redSlider.value = r;
    self.greenSlider.value = g;
    self.blueSlider.value = b;
    self.alplaSlider.value = a;
    
    self.redSliderLabel.text = [NSString stringWithFormat:@"Red:%.1f", r * 255];
    self.greenSliderLabel.text = [NSString stringWithFormat:@"Green:%.1f", g * 255];
    self.blueSliderLabel.text = [NSString stringWithFormat:@"Blue:%.1f", b * 255];
    self.alplaSliderLabel.text = [NSString stringWithFormat:@"Alpha:%.1f", a * 255];
}

- (UIColor *)getColor
{
    CGFloat r = self.redSlider.value;
    CGFloat g = self.greenSlider.value;
    CGFloat b = self.blueSlider.value;
    CGFloat a = self.alplaSlider.value;
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

- (void)valueChanged:(UISlider *)slider
{
    UILabel *label = [slider.superview viewWithTag:2];
    NSString *prefix = [[label.text componentsSeparatedByString:@":"] firstObject];
    label.text = [NSString stringWithFormat:@"%@:%.1f", prefix, slider.value * 255];
    if (self.onColorChanged)
    {
        self.onColorChanged(self);
    }
}

@end
